<%@page import="weaver.general.Util"%>
<%@page import="java.util.LinkedHashMap"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.util.Map"%>
<%@page import="java.sql.ResultSetMetaData"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="weaver.conn.ConnectionPool"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%!

public String basisJavaBeanCode(String packagePath,String className,Map<String,Map<String,String>> map){
	StringBuffer sb = new StringBuffer();
	if( map != null ){
		for(Map.Entry<String, Map<String,String>> entry: map.entrySet()){
			String tableName = "".equals(Util.null2String(className))? firstCharToUpperCase(entry.getKey()) : firstCharToUpperCase(className);
			if(packagePath!=null && !"".equals(packagePath))
				sb.append("package ").append(packagePath.replaceAll("\\\\", ".").replaceAll("/", ".")).append(";").append("\n");
			sb.append("public class ").append(firstCharToUpperCase(tableName)).append(" {").append("\n");
			for(Map.Entry<String,String> entryField : entry.getValue().entrySet()){
				String key = entryField.getKey();
				String firstCharToLowerKey = firstCharToLowerCase(key);	//首字母小写
				String firstCharToUpperKey = firstCharToUpperCase(key);	//首字母大写
				String value = entryField.getValue();
				sb.append("\n");
				sb.append("\t").append("private ").append(value).append(" ").append(firstCharToLowerKey).append(";").append("\n");
				sb.append("\t").append("public ").append("void").append(" set").append(firstCharToUpperKey).append("(").append(value).append(" ").append(firstCharToLowerKey).append(")").append("{").append("\n");
				sb.append("\t\t").append("this.").append(firstCharToLowerKey).append(" = ").append(firstCharToLowerKey).append(";").append("\n");
				sb.append("\t").append("}").append("\n");
				String get = "get"; if(value.indexOf("boolean")>=0 || value.indexOf("Boolean")>=0) get = "is";
				sb.append("\t").append("public ").append(value).append(" ").append(get).append(firstCharToUpperKey).append("()").append("{").append("\n");
				sb.append("\t\t").append("return this.").append(firstCharToLowerKey).append(";").append("\n");
				sb.append("\t").append("}").append("\n");
			}
			sb.append("}").append("\n");
		}
	}else{
		sb.append("数据表不存在!");
	}
	return sb.toString();
}

public String jfinalModelCode(String packagePath,String className,Map<String,Map<String,String>> map){
	StringBuffer sb = new StringBuffer();
	if( map != null ){
		for(Map.Entry<String, Map<String,String>> entry: map.entrySet()){
			String tableName = "".equals(Util.null2String(className))? firstCharToUpperCase(entry.getKey()) : firstCharToUpperCase(className);
			String beanModelName = "Bean"+tableName;
			String modelName = tableName+"Model";
			
			if(packagePath!=null && !"".equals(packagePath))
				sb.append("package ").append(packagePath.replaceAll("\\\\", ".").replaceAll("/", ".")).append(";").append("\n");
			sb.append("\n");
			sb.append("import com.jfinal.plugin.activerecord.IBean;").append("\n");
			sb.append("import com.jfinal.plugin.activerecord.Model;").append("\n");
			sb.append("\n");
			sb.append("@SuppressWarnings(\"serial\")").append("\n");
			sb.append("public class ").append(modelName).append(" extends ").append(beanModelName).append("<").append(modelName).append(">{").append("\n");
			sb.append("\t").append("public static final ").append(modelName).append(" dao = new ").append(modelName).append("();").append("\n");
			sb.append("\t").append("public static final String TABLE_NAME = \"").append(entry.getKey()).append("\";").append("\n");
			sb.append("}").append("\n");
			sb.append("\n");
			sb.append("@SuppressWarnings(\"serial\")").append("\n");
			sb.append("abstract class ").append(beanModelName).append("<M extends ").append(beanModelName).append("<M>> extends Model<M> implements IBean{").append("\n");
			for(Map.Entry<String,String> entryField : entry.getValue().entrySet()){
				String key = entryField.getKey();
				String firstCharToLowerKey = firstCharToLowerCase(key);	//首字母小写
				String firstCharToUpperKey = firstCharToUpperCase(key);	//首字母大写
				String value = entryField.getValue();
				
				sb.append("\t").append("public ").append("void").append(" set").append(firstCharToUpperKey).append("(").append(value).append(" ").append(firstCharToLowerKey).append(")").append("{").append("\n");
				sb.append("\t\t").append("set(\"").append(key).append("\"").append(",").append(firstCharToLowerKey).append(");").append("\n");
				sb.append("\t").append("}").append("\n");
				String get = "get"; if(value.indexOf("boolean")>=0 || value.indexOf("Boolean")>=0) get = "is";
				sb.append("\t").append("public ").append(value).append(" ").append(get).append(firstCharToUpperKey).append("()").append("{").append("\n");
				sb.append("\t\t").append("return get(\"").append(key).append("\");").append("\n");
				sb.append("\t").append("}").append("\n");
			}
			sb.append("}").append("\n");
		}
	}else{
		sb.append("数据表不存在!");
	}
	return sb.toString();
}


/**
 * 首字母变小写
 */
public String firstCharToLowerCase(String str) {
	char firstChar = str.charAt(0);
	if (firstChar >= 'A' && firstChar <= 'Z') {
		char[] arr = str.toCharArray();
		arr[0] += ('a' - 'A');
		return new String(arr);
	}
	return str;
}

/**
 * 首字母变大写
 */
public String firstCharToUpperCase(String str) {
	char firstChar = str.charAt(0);
	if (firstChar >= 'a' && firstChar <= 'z') {
		char[] arr = str.toCharArray();
		arr[0] -= ('a' - 'A');
		return new String(arr);
	}
	return str;
}

public Map<String,Map<String,String>> findTableFieldMap(String tableName){
	Connection conn = getConnection();
	Map<String,Map<String,String>> map = null;
	PreparedStatement pst = null;
	ResultSet rs = null;
	ResultSetMetaData rsmd = null;
	try{
		if(conn!=null){
			String sql = " select * from " + tableName + " where 1<>1 ";
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			rsmd = rs.getMetaData();
			if(rsmd!=null){
				map = new LinkedHashMap<String,Map<String,String>>();
				Map<String,String> fieldMap = new LinkedHashMap<String,String>();
				int count = rsmd.getColumnCount();
				for( int i=1; i<=count; i++){
					String fieldName = rsmd.getColumnLabel(i);
					String fieldClass = rsmd.getColumnClassName(i);
					fieldMap.put(fieldName,fieldClass);
				}
				map.put(tableName,fieldMap);
			}
		}
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		close(rs,pst,conn);
	}
	return map;
}

private Connection getConnection(){
	return ConnectionPool.getInstance().getConnection();
}

private void close(ResultSet rs, Statement st,Connection conn) {
	if (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}
	if (st != null) {try {st.close();} catch (SQLException e) {e.printStackTrace();}}
	if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}
}
%>
